package com.graduationdesign.zengyan.graduationdesignapp.ui.presenter;

import android.support.annotation.NonNull;

import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.AccountManager;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.MainService;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseLoadingView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.ErrorConsumer;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.Transforms;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.LoginContact;

import java.util.HashMap;

import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by ZengYan on 2018/4/19.
 *
 * @desc :
 */
public class LoginPresenter implements LoginContact.Presenter {

    private final LoginContact.View mLoginView;
    private final MainService mMainService;
    private final CompositeDisposable compositeDisposable;
    private final ErrorConsumer mErrorConsumer;

    public LoginPresenter(@NonNull LoginContact.View mLoginView, @NonNull BaseErrorView errorView , @NonNull MainService mainService) {
        this.mLoginView = mLoginView;
        this.mMainService = mainService;
        this.compositeDisposable = new CompositeDisposable();
        this.mErrorConsumer = new ErrorConsumer(errorView,"LoginPresenter");

    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unSubsrible() {
        this.compositeDisposable.clear();
    }

    @Override
    public void login(String stunum, String pwd, BaseLoadingView loadingView) {
        HashMap<String, String> map = new HashMap<>();
        map.put("studentNum", stunum);
        map.put("password", pwd);
        mMainService
                .login(map)
                .compose(Transforms.applyAll(loadingView, compositeDisposable))
                .subscribe(userResult -> {
                    AccountManager.getInstance().setCurrentUser(userResult.getData());
                    mLoginView.showLoginSuccess(userResult.getData());

                }, mErrorConsumer);

    }

}
